<?php
class Backend {

    var $module = array();
	var $submodule = array();

    function Backend() {

    }

    function get_all_module() {

		for($i = 0; $i < count($this->module); $i++){
			for($j = 0; $j < count($this->module)-1; $j++){
				if($this->module[$j+1]['order'] < $this->module[$j]['order']){
					$tmp = $this->module[$j];
					$this->module[$j] = $this->module[$j+1];
					$this->module[$j+1] = $tmp;
				}
			}
		}

		return $this->module;
    }

	function get_all_submodule(){
		return $this->submodule;
	}

	function get_title($m, $p){

		if($p != ""){
			$sub = explode("/",$p);

			if(!strstr($sub[0], ".php")){
				foreach($this->submodule as $k => $v){
					if($v['module'] == $m && $v['sub'] == $sub[0]){
						return $v['title'];
					}
				}
			}
		}

		foreach($this->module as $k => $v){
			if($v['module'] == $m){
				return $v['title'];
			}
		}

		return false;
	}

	function add_module($arr){

		$m = array();

		foreach($arr as $key => $value){
			switch ($key) {
				case "module" : case "order" : case "title" : case "file" : $m[$key] = $value; break;
				default : 
					$value['module'] = $arr['module'];
					$value['sub'] = $key;
					$this->add_submodule($value); break;
			}
		}

		$m['link'] = URL ."admin/" . $m['module'];

		array_push($this->module, $m);
	}

	function add_submodule($arr){
		$arr['link'] = URL ."admin/" . $arr['module']."/". $arr['sub'];
		array_push($this->submodule, $arr);
	}

    function get_path($m, $p) {

        if($p != ""){
			if(strstr($p, ".php")){
				$path = MOD . $m . "/" . $p;
			}else{
				foreach($this->submodule as $k => $v){
					if($v['module'] == $m && $v['sub'] == $p){
						$path = MOD . $m . "/" . $v['file'];
					}
				}
			}
        }else{

			foreach($this->module as $k => $v){
				if($v['module'] == $m){
					$path = MOD . $m . "/". $v['file'];
				}
			}
        }

		if(file_exists($path)){
			return $path;
		}

        return false;
    }
}
?>